<?php

namespace App\Http\Controllers\Agent;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\GuildUser;
use App\Service\GuildUserService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Pagination\LengthAwarePaginator;
use Excel;

class GuildController extends Controller
{
    public $v = 'agent.guild.';
	public $paginate = 15;

	/**
	 * 邀请主播
	 * @param Request $request
	 * @return type
	 */
	public function index(Request $request){
		$prom = require_once base_path('storage').'/app/system/prom.php';
        return view($this->v . 'index', compact('prom'));
	}
	/**
	 * 主播列表
	 * @param 
	 * @param 
	 * @return type
	 */
	public function ganchor(Request $request) {
		$gid = $request->user('agent')->gid;
		$anchor = [];
		$keyword = $request->query('keyword');
		$status = $request->query('status') ?? 1;
		//$perPage = 3;
        //当前页数 默认1
        $page = $request->page ?: 1;
        //计算每页分页的初始位置
        $offset = ($page * $this->paginate) - $this->paginate;
		$rest = (new \App\Service\ApiService())->getAnchor(['gid' => $gid, 'search' => $keyword, 'status' => $status]);
		if($rest['total'] != 0) {
			$anchor= new LengthAwarePaginator(array_slice($rest['info'], $offset, $this->paginate, true), $rest['total'], $this->paginate,
				$page, ['path' => $request->url(), 'query' => $request->query()]);
		}       
		return view($this->v . 'ganchor', compact('anchor', 'gid', 'keyword', 'status'));
	}
	/**
     * 公会收益明细
     * @param
     * @return
     */
    public function earninglist(Request $request){
        $earning = [];
        $guild = Auth::guard('agent')->user()->gid;
        $start_time = $request->query('start_time');
        //$perPage = 3;
        //当前页数 默认1
        $page = $request->page ?: 1;
        //计算每页分页的初始位置
        $offset = ($page * $this->paginate) - $this->paginate;
        $rest = (new \App\Service\ApiService())->getEarning(['guild' => $guild, 'start_time' => $start_time]);
        if($rest['count'] != 0) {
            $earning = new LengthAwarePaginator(array_slice($rest['list'], $offset, $this->paginate, true), $rest['count'], $this->paginate,
                $page, ['path' => $request->url(), 'query' => $request->query()]);
        }
        return view($this->v . 'earninglist', compact('start_time', 'earning'));
    }
	/**
	 * 公会基本信息
	 * @param Request $request
	 */
	public function information(Request $request){
		$guild = Auth::guard('agent')->user()->gid;
		$ginfo = GuildUser::where('gid', $guild)->first();
		return view($this->v . 'information', compact('ginfo'));
	}
	/**
	 * 主播收益明细导出
	 * @param
	 * @return
	 */
	public function exportearning(Request $request) {
        $guild = Auth::guard('agent')->user()->gid;
        $start_time = $request->query('start_time');
        $rest = (new \App\Service\ApiService())->getEarning(['guild' => $guild, 'start_time' => $start_time]);
		$cellData[0] = ['时间','公会ID/名称','礼物收入（洽贝）','语音收入（洽贝）','视频收入（洽贝）','邮票收入（洽贝）','收入提成比例','公会收入(元)'];
		foreach($rest['list'] as $k=>$v) {
			$cellData[$k+1][] = $v['date'];
			$cellData[$k+1][] = $v['guild'].'/'.$v['title'];
			$cellData[$k+1][] = $v['sum_gift'];
			$cellData[$k+1][] = $v['sum_voice'];
			$cellData[$k+1][] = $v['sum_live'];
			$cellData[$k+1][] = $v['sum_stamp'];
			$cellData[$k+1][] = $v['cale'];
			$cellData[$k+1][] = $v['sum_guild'];
			unset($rest['list'][$k]);
		}
		Excel::create("主播收益明细-".date('YmdHis'),function ($excel) use ($cellData){
			$excel->sheet('score',function ($sheet) use ($cellData) {
				$sheet->rows($cellData);
				ob_end_clean();
			});
		})->export('csv');
	}
}